package com.example.hzit.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class MyRequestInterceptorConfig {

	@Bean
	public RequestInterceptor requestInterceptor(){
		return requestTemplate -> {
			// 1. 得到原来的请求
			ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			HttpServletRequest request = requestAttributes.getRequest();
			// 2. 得到原来的请求头
			String authorization = request.getHeader("Authorization");
			// 3. 将其放到新的请求头中
			requestTemplate.header("Authorization",authorization);
		};
	}
}
